function getObservation(url){

var ajax_load = "<img class='loading' src='./views/images/loading.gif'   alt='loading...' />";
//$('#datasetvalue').html(ajax_load);
$('#measurevalue').html(ajax_load);
$('#observationvalue').html(ajax_load);
$('#attributevalue').html(ajax_load);
$('#dimensionvalue').html(ajax_load);


var param=getParameters(url.toString());

$.ajax({
    url: './index.php',
    type: 'POST',
	data:"uri="+param[0]+"&endpoint="+param[1]+"&rt=uri/view",
    timeout: 1000,
	dataType:"json",
    error: function(){
        alert('No Observation exists...');
    },
    success: function(responseText){
	if(responseText.status==="ok"){
     fillInterfaceWithJsonObject(responseText.observation,param);
	}
	else
	{
      alert('No Observation exists...');
	}
    }
});



$.ajax({
	url:'./index.php',
	type:'POST',
	data:'uri='+param[0]+'&endpoint='+param[1]+'&rt=uri/dataset',
	timeout: 1000,
	dataType:"json",
	error: function(){
        alert('No dataset exists...');
    },
    success: function(responseText){
		if(responseText.status==="ok"){
		createDatasetSlidePanel(responseText.dataset,param)
		}
		else
		{
		alert('No Info exists...');
		}
	   
    }
});
	
}

function createDatasetSlidePanel(dataset,param){
    $('#datasetvalue').append(
	$(document.createElement('a'))
	.attr('id','trigger1')
	.addClass('trigger')
	.html(dataset.label)
	);
    $('#datasetvalue').append(
	$(document.createElement('div'))
	.attr('id','panel')
	.addClass('panel')
	.html('<table width="75%" class="dimensiontable"><tr><td>Publisher:</td><td>'+dataset.publisher+'</td></tr><tr><td colspan="2" align="right"><a href="./index.php?uri='+dataset.dsuri+'&endpoint='+param[1]+'&rt=dataset" >More..</a></td></tr></table>'));
    $("#trigger1").click(function(){
	$("#panel").slideToggle("slow");
	 return false;
	});	

}

function fillInterfaceWithJsonObject(observation,param){
  
getMeasure(observation,param);
$('#observationvalue').html("<h1 class=\"observation\"><span>"+observation.observationValue+"</span></h1>");
$('#attributevalue').html("<br/>");
$('#dimensionvalue').html("<p></p>");

getAttributes(observation);
getDimensions(observation,param);
$('#param').append("<input name=\"uri\" id=\"uri\" type=\"hidden\" value=\""+param[0]+"\" />");
$('#param').append("<input name=\"endpoint\" id=\"endpoint\" type=\"hidden\" value=\""+param[1]+"\" />");

}

function getCssClass(value){
	if (value%2 == 0)
		return "tr-even";
	else
		return "tr-odd";
}

function getMeasure(observation,param){
     $('#measurevalue').html("");
	 var table=$(document.createElement('table')).addClass('measuretable').width('100%');
	 var tr=$(document.createElement('tr')).width('100%').hover(function ()
			{ $(this).addClass("hover");},  function () {$(this).removeClass("hover");});
     var td1=$(document.createElement('td')).addClass('measure');
	
	 var left_arr=$(document.createElement('img')).attr('src','./views/images/left.gif').click(function () {traversemeasure('pre');});
	 var right_arr=$(document.createElement('img')).attr('src','./views/images/right.gif').click(function () {traversemeasure('next');});
	 td1.append(left_arr);
     var an=$(document.createElement('a'))
		 .attr('id','measure')
		 .attr('href','./index.php?uri='+observation.measure.URI.replace('#','%23')+'&endpoint='+param[1]+'&rt=measure')
		 ;
	 an.html(observation.measure.label);
	
	 td1.append(an);
	 td1.append(
	 $(document.createElement('input'))
		 .attr('type','hidden')
		 .val(observation.measure.label)
		 .attr('id','measurelab')
	 );
     td1.append(
	 $(document.createElement('input'))
		 .attr('type','hidden')
		 .val(observation.measure.URI)
		 .attr('id','measureURI')
	 );


	 td1.append(right_arr);

	 tr.append(td1);
	 
	 table.append(tr);

	$('#measurevalue').append(table);




}
function getDimensions(observation,param){
   var table=$(document.createElement('table')).addClass('dimensiontable').attr('align','center');
	for (var i=0; i<observation.dimensions.length; i++) {
		
        var tr=$(document.createElement('tr')).addClass(getCssClass(i)).hover(function ()
			{ $(this).addClass("hover");},  function () {$(this).removeClass("hover");});
        var td1=$(document.createElement('td'));
        var td2=$(document.createElement('td')).attr('id',observation.dimensions[i].URI);
		td1.append("<a href='./index.php?uri="+observation.dimensions[i].URI.replace('#','%23')+"&endpoint="+param[1]+"&rt=dimension'><h5>"+observation.dimensions[i].label+":</h5></>");
		var isIndirect="";
		if(observation.dimensions[i].isIndirect==true) isIndirect="0"; else isIndirect="1";
        if(observation.dimensionValue[i]!=null){
			if(observation.dimensionValue[i].value!=""){
					var tr2=$(document.createElement('tr'));
					var td3=$(document.createElement('td'));
					createDropDown(td2,td3,i,observation.dimensions[i].URI,param[1],observation);
					
			}
			else 
			{
			
			td2.append(observation.dimensionValue[i].valueURI);
			
			}
        td2.append( "<input type='hidden' name='dimval' value='"+observation.dimensionValue[i].value+"' ></input>");
		td2.append( "<input type='hidden' name='dimvaluri' value='"+observation.dimensionValue[i].valueURI+"' ></input>");
		
        }
		else
		{
		td2.append( "<input type='hidden' name='dimval' value='' ></input>");
		td2.append( "<input type='hidden' name='dimvaluri' value='' ></input>");
		
		
		}
		td2.append( "<input type='hidden' name='dimuri' value='"+observation.dimensions[i].URI+"'></input>");
		td2.append( "<input type='hidden' name='dimlab' value='"+observation.dimensions[i].label+"'></input>");
	
		td2.append( "<input type='hidden' name='dimindirect' value='"+isIndirect+"'></input>");
		td2.append( "<input type='hidden' name='dimatt' value='"+observation.dimensions[i].attachmentLevel+"' ></input>");
		td2.append( "<input type='hidden' name='dimcomment' value='"+observation.dimensions[i].comment+"' ></input>");
		tr.append(td1);
		tr.append(td2);
		tr2.append(document.createElement('td'));
		tr2.append(td3);
        table.append(tr);
        table.append(tr2);
	
	}
	$('#dimensionvalue').append(table);
	
}

function getAttributes(observation){
   var table=$(document.createElement('table'));
	for (var i=0; i<observation.attributes.length; i++) {
        var tr=$(document.createElement('tr'));
        var td=$(document.createElement('td')).attr('id','td_'+i);
       
		if(observation.attributes[i].label!=null){
		td.append("<h5 class=\"dimension\">"+observation.attributes[i].label+":</h5>");
		}
		var indirect;
		if(observation.attributes[i].isIndirect==true)
			indirect="0";
		else
			indirect="1";
        if(observation.attributeValue[i]!=null){
				if(observation.attributeValue[i].value!=""){
						td.append(observation.attributeValue[i].value);
				}
				else 
				{
						td.append(observation.attributeValue[i].valueURI);
				
				}
        td.append("<input type='hidden' name='attval' value='"+observation.attributeValue[i].value+"' ></input>");
		td.append("<input type='hidden' name='attvaluri' value='"+observation.attributeValue[i].valueURI+"' ></input>");
	
		}
		else {
		td.append("<input type='hidden' name='attval' value='' ></input>");
		td.append("<input type='hidden' name='attvaluri' value='' ></input>");
	
		}
		td.append("<input type='hidden' name='atturi' value='"+observation.attributes[i].URI+"'></input>");
		td.append("<input type='hidden' name='attlab' value='"+observation.attributes[i].label+"'></input>");
	
		td.append("<input type='hidden' name='attindirect' value='"+indirect+"'></input>");
		
		tr.append(td);
        table.append(tr);
	
	}
	$('#attributevalue').append(table);
}
function getParameters(url){
var arr=new Array();
 var split1=url.split('?');
 var split2=split1[1].split('&');
 for(i=0;i<split2.length;i++){
    var split3=split2[i].split('=');
	if(split3[0]=="uri"){
	  arr[0]=split3[1];
	}
	if(split3[0]=="endpoint"){
	  arr[1]=split3[1];
	}
	if(split3[0]=="rt"){
	  arr[2]=split3[1];
	}
 }

return arr;
}

function traverse(dim_val,uri,obs_uri,dir){
action=dir;



//alert(dim_val);
var arr_dimuri=document.getElementsByName('dimuri');
var arr_dimlab=document.getElementsByName('dimlab');
var arr_dimval=document.getElementsByName('dimval');
var arr_dimvaluri=document.getElementsByName('dimvaluri');
var arr_dimindirect=document.getElementsByName('dimindirect');
var arr_dimatt=document.getElementsByName('dimatt');
var arr_comment=document.getElementsByName('dimcomment');

var measureURI=$('#measureURI').val();
var measurelab=$('#measurelab').val();

var str="rt=uri/traverse"
+"&action="+action
+"&uri="+obs_uri
+"&endpoint="+$('#endpoint').val()
+"&dim_uri="+uri
+"&dim_val="+dim_val
+"&arr_dimuri="+toArray(arr_dimuri).toString()
+"&arr_dimval="+toArray(arr_dimval).toString()
+"&arr_dimlab="+toArray(arr_dimlab).toString()
+"&arr_dimindirect="+toArray(arr_dimindirect).toString()
+"&arr_dimatt="+toArray(arr_dimatt).toString()
+"&arr_comment="+toArray(arr_comment).toString()
+"&measureuri="+measureURI
+"&measurelab="+measurelab
+"&arr_dimvaluri="+toArray(arr_dimvaluri).toString();


var ajax_load = "<img class='loading' src='./views/images/loading.gif'  alt='loading...' />";
$('#datasetvalue').html(ajax_load);
$('#measurevalue').html(ajax_load);
$('#observationvalue').html(ajax_load);
$('#attributevalue').html(ajax_load);
$('#dimensionvalue').html(ajax_load);

$.ajax({
    url: './index.php',
	data:str,
    type: 'POST',
    timeout: 1000,
	dataType:"json",
    error: function(o){
        alert('No Observation exists...'+o.uri);
		//window.location="./index.php?uri="+obs_uri.replace('#','%23')+"&endpoint="+document.getElementById('endpoint').value+"&rt=uri";
    },
    success: function(o){
    if(o.status==="ok"){
	   window.location="./index.php?uri="+o.uri.replace('#','%23')+"&endpoint="+document.getElementById('endpoint').value+"&rt=uri";
	 }
	 else
		{
	 window.location="./index.php?uri="+obs_uri.replace('#','%23')+"&endpoint="+document.getElementById('endpoint').value+"&rt=uri";
	 }

    }
	
	
});
return false;
}


function traversemeasure(dir){

action='next';
if(dir=='next'){
action='next';
}
else{
action='pre';
}

var arr_dimuri=document.getElementsByName('dimuri');
var arr_dimlab=document.getElementsByName('dimlab');
var arr_dimval=document.getElementsByName('dimval');
var arr_dimvaluri=document.getElementsByName('dimvaluri');
var arr_dimindirect=document.getElementsByName('dimindirect');
var arr_dimatt=document.getElementsByName('dimatt');
var arr_comment=document.getElementsByName('dimcomment');

var measureURI=$('#measureURI').val();
var measurelab=$('#measurelab').val();

var str="rt=uri/traversemeasure"
+"&action="+action
+"&uri="+$('#uri').val()
+"&endpoint="+$('#endpoint').val()

+"&arr_dimuri="+toArray(arr_dimuri).toString()
+"&arr_dimval="+toArray(arr_dimval).toString()
+"&arr_dimlab="+toArray(arr_dimlab).toString()
+"&arr_dimindirect="+toArray(arr_dimindirect).toString()
+"&arr_dimatt="+toArray(arr_dimatt).toString()
+"&arr_comment="+toArray(arr_comment).toString()
+"&measureuri="+measureURI
+"&measurelab="+measurelab
+"&arr_dimvaluri="+toArray(arr_dimvaluri).toString();


var ajax_load = "<img class='loading' src='./views/images/loading.gif'  alt='loading...' />";
$('#datasetvalue').html(ajax_load);
$('#measurevalue').html(ajax_load);
$('#observationvalue').html(ajax_load);
$('#attributevalue').html(ajax_load);
$('#dimensionvalue').html(ajax_load);

$.ajax({
    url: './index.php',
	data:str,
    type: 'POST',
    timeout: 1000,
	dataType:"json",
    error: function(o){
		window.location="./index.php?uri="+$('#uri').val().replace('#','%23')+"&endpoint="+$('#endpoint').val()+"&rt=uri";
    },
    success: function(o){
    if(o.status==="ok"){
	 window.location="./index.php?uri="+o.uri.replace('#','%23')+"&endpoint="+$('#endpoint').val()+"&rt=uri";
	 }
	 else{
	 	window.location="./index.php?uri="+$('#uri').val().replace('#','%23')+"&endpoint="+$('#endpoint').val()+"&rt=uri";
	 }

    }
	
	
});
return false;

}
function toArray(obj) {
  var array = [];
  // iterate backwards ensuring that length is an UInt32
  for (var i = obj.length >>> 0; i--;) { 
    array[i] = obj[i].value;
  }
  return array;
}

function createDropDown(td2,td3,i,dim_uri,endpoint,observation)
{


var str="rt=dimension/allvalues"
+"&uri="+dim_uri.replace('#','%23')
+"&endpoint="+endpoint;
var values=[];
$.ajax({
    url: './index.php',
	data:str,
    type: 'POST',
    timeout: 1000,
	dataType:"json",
    error: function(){
		alert('Error in Ajax');
    },
    success: function(responseText){
     
	if(responseText.status==="ok"){
     values= responseText.values;
	 if(values.length>1){
	            var dropdown=$(document.createElement('select')).addClass('mySelectBoxClass').attr('id','dropdown_'+i);
	            for(var j=0;j<values.length;j++){ 
                    
                    if(values[j].value==observation.dimensionValue[i].value){ 
					   
						dropdown.append(
						$('<option>')
						.html(values[j].value)
						.val(values[j].value)
						.attr('selected','selected')
						);
					}
					else{
					    dropdown.append(
						$('<option>')
						.html(values[j].value)
						.val(values[j].value)
						);
					}
   
			    }
				dropdown.bind('change',function(event) {
					    var me = $(event.target).val();
						traverse(me,dim_uri,observation.observationURI,'none');
						}
					);
				
				var left_arr=$(document.createElement('img'))
					.attr('src','./views/images/left.gif')
					.bind('click',{i:i} ,function(event) {
						traverse(observation.dimensionValue[event.data.i].value,dim_uri,observation.observationURI,'pre');}
				   );
               
				var right_arr=$(document.createElement('img'))
					.attr('src','./views/images/right.gif')
					.bind('click',{i:i} ,function(event) {
						traverse(observation.dimensionValue[event.data.i].value,dim_uri,observation.observationURI,'next');}
				   );
					td2.append(left_arr);
					td2.append(dropdown);
					td2.append(right_arr);
	 
				dropdown.customStyle();	
						
				
					
		}
     else{
	 				td2.append(observation.dimensionValue[i].value);
	 }		
	}
	else
	{
      alert('No Values exists...');
	}

    }
	});


}
